package com.alick.learnwebrtc.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.ruoyi.app.R
import com.alick.learnwebrtc.bean.Contact
import com.alick.learnwebrtc.manager.AccountManager
import com.ruoyi.app.databinding.ItemContactBinding

class ContactsAdapter(private val list: MutableList<Contact>) :
    RecyclerView.Adapter<ContactViewHolder>() {

    var onClickVideo: ((Contact) -> Unit)? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ContactViewHolder {
//        return ContactViewHolder(
//            LayoutInflater.from(parent.context).inflate(R.layout.item_contact, parent, false)
//        )

        val binding = ItemContactBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return ContactViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ContactViewHolder, position: Int) {
        val contact = list[position]
        holder.binding.tvAccount.text = contact.account
//        holder.itemView.tvAccount.text = contact.account

        if (contact.account == AccountManager.getAccount()) {
            holder.binding.btnVideo.visibility = View.GONE// holder.itemView.btnVideo.visibility = View.GONE
        } else {
//            holder.itemView.btnVideo.visibility = View.VISIBLE
//            holder.itemView.btnVideo.setOnClickListener {
//                onClickVideo?.invoke(contact)
//            }
            holder.binding.btnVideo.visibility = View.VISIBLE
            holder.binding.btnVideo.setOnClickListener {
                onClickVideo?.invoke(contact)
            }
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }
}
class ContactViewHolder(val binding: ItemContactBinding) :
    RecyclerView.ViewHolder(binding.root)
//class ContactViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)